<?php
//同步数据到另一个数据库
//翻译数据在另外一步执行
include_once("../../config.php");
$host = $_SERVER['HTTP_HOST']; //取得进入所输入的域名
$theme_folder = host_theme($host);
include_once '../../front/' . $theme_folder . '/connection.php';
?>



<?include_once("bdsdk.php");//导入百度的sdk?>






<?php
//生成unid项的功能已经做在了update_table
//sync_datas.php?from=en&to=zh&table=term&nid=473
//from=en  初始语言，默认英语，可以不用改
//to翻译目标语言
//table翻译site，detail数据还是term分类
//nid数据的nid，最后更新的时候以unid做标准的。

//nid数据的nid，最后更新的时候以unid做标准的。
//detail同步的时候，请在js中调用的时候，分两步调用。先sync_datas.php，然后再调用先sync_datas_detail.php，补充翻译detail项的内容


if(!$_REQUEST['from']) {$_REQUEST['from'] ="zh";}
if(!$_REQUEST['to'])   {$_REQUEST['to']   ="en";}
if(!$_REQUEST['table']){$_REQUEST['table']="site";}


//echo  $_REQUEST['from'] ;
//echo  $_REQUEST['to'] ;
//$_REQUEST['table'] = "site";
//$_REQUEST['nid']   = "5485";


$la2db['en']  = "huanqiao2";
$la2db['ara'] = "huanqiao2_ara";
$la2db['fra'] = "huanqiao2_fra";
$la2db['hi']  = "huanqiao2_hi";
$la2db['jp']  = "huanqiao2_jp";
$la2db['pl']  = "huanqiao2_pl";
$la2db['pt']  = "huanqiao2_pt";
$la2db['ru']  = "huanqiao2_ru";
$la2db['spa'] = "huanqiao2_spa";
$la2db['th']  = "huanqiao2_th";
$la2db['vie']  = "huanqiao2_vn";//特别注意，百度的越南语参数是vie，不是vn，数据库保持
$la2db['zh']  = "huanqiao2_ara";





//***********************
//取出要翻译的数据
//**********************

if($_REQUEST['table']=="site")
{
   //传的是site就用这个
   $_target=select_lines_items($table_prefix."site","","name,title,description,keyword"," 1 = 1","");
}

if($_REQUEST['table']=="detail")
{
   //echo "传的是产品";
   $_target=select_lines_items($table_prefix."detail","","name,title,description,keyword,model,introduce,detail"," nid = '".$_REQUEST['nid']."' ","");

   //这部分不需要翻译的，但是要同步一下一些设置，排序之类的。
   $_target_attr=select_one_line_all_items($table_prefix."detail"," nid = '".$_REQUEST['nid']."' ");




//请补充一下同步的分类关系
//先取出英文的从属关系

    $_relations=select_lines_all_items($table_prefix."relation","","detail_nid = '".$_REQUEST['nid']."'","");

    print_r2($_relations);
//exit;


    $term_nid_list= transform_2darray_in_1darray_number_as_key($_relations,"term_nid");
    $term_nid_list_str=implode("','",$term_nid_list);
#$term_unid=select_lines_all_items($table_prefix."term","","nid in ('".$term_nid_list_str."')","");
    $term_unid=select_lines_items($table_prefix."term","","unid","nid in ('".$term_nid_list_str."')","");

//Array
//(
//    [0] => Array
//    (
//        [unid] => 202108281618541232463837
//    )
//
//)


//所有的分类的unid
echo "查找到的unid";
print_r2($term_unid);
//这个数据的unid
//print_r2($_target_attr['unid']);


//下面两步，交给更换数据后去处理
//在新数据库找出对应的term id 和detail id
//插入relation


}






if($_REQUEST['table']=="term")
{
   //传的是site就用这个
   $_target      = select_lines_items($table_prefix."term","","name,title,description,keyword,mid,introduce","nid = '".$_REQUEST['nid']."'","");
   $_target_attr = select_one_line_all_items($table_prefix."term","nid = '".$_REQUEST['nid']."'");
}


//echo "_target";
//print_r2($_target);






//***********************
//取出的数据合并成一篇。再打散
//**********************


//之前是逐个项翻译，但是百度限制一秒翻译一个，所以我们合并一下

$_target_pure_str="";//detail内容，纯文字，合并用  @_@做接合

foreach($_target[0] as $k=>$v)
{
    if($k!="detail")
    {
        $_target_pure_str=$_target_pure_str."✪".$v;
    }
}


//echo "合并后的原文内容";
//echo $_target_pure_str;


$_result_json=translate($_target_pure_str,$_REQUEST['from'],$_REQUEST['to']);

//echo "翻译后的内容";
//print_r2( $_result_json['trans_result']);


$tran2=explode("✪", $_result_json['trans_result'][0]['dst']);
$tran_txt_array=array_splice($tran2,1);

//和原文数组同序列的数组

$i=0;
foreach($_target[0] as $k=>$v)
{
    $_target_attr[$k]=$tran_txt_array[$i];
    $i++;
}



//echo "翻译后的内容数组";
//print_r2($_target_attr);






//***********************
//循环替换生成新的数据，
//用于更新或者插入
//更新插入都以uuid做标准
//relation也要同时更新一下，没有对应的，就跳过。提醒。
//***********************



//20210823为了节省空间，这里把uploadfile的文件夹，都做一个软链接，以huanqiao2做基础？不过因为要顾及原来的图片，所以搁置。

//print_r($_target_attr);
//这里补充一下要转移的图片，然后复制过去。

$trans_img[]=$_target_attr['icon'];
$trans_img[]=$_target_attr['image1'];
$trans_img[]=$_target_attr['image2'];
$trans_img[]=$_target_attr['image3'];
$trans_img[]=$_target_attr['image4'];
$trans_img[]=$_target_attr['image5'];
$trans_img[]=$_target_attr['extend_img'];
$trans_img[]=$_target_attr['banner'];
$trans_img[]=$_target_attr['attachment'];
$trans_img[]=$_target_attr['image6'];
$trans_img[]=$_target_attr['image7'];
$trans_img[]=$_target_attr['image8'];
$trans_img[]=$_target_attr['image9'];
$trans_img[]=$_target_attr['image10'];
$trans_img[]=$_target_attr['en_name'];
$trans_img[]=$_target_attr['video_img'];
$trans_img[]=$_target_attr['video_img2'];


foreach($trans_img as $k=>$v)
{
    $_source=ROOT_PATH_."/uploadfiles/".$theme_folder."/".$v;
    $_destin=ROOT_PATH_."/uploadfiles/".str_replace("_","",$la2db[$_REQUEST['to']])."/".$v;
    //echo "<br/>";
    copy($_source,$_destin);
    //echo "<br/>";
}







foreach($_target_attr as $k=>$v)
{

    //and ($k!="unid") 排除unid就不能插入相同该数据了
    if(($k!="nid") and ($k!="inputtime")  and ($k!="modifytime"))
    {
       $_target_attr_ai['ai_'.$k]=$v;
    }


}





foreach($_result as $k=>$v)
{
    $_target_attr_ai[$k]=$v;
}



//print_r2($_target_attr_ai);















//$_result是已经翻译的，然后就去更新就可以了。
//循环设置新的数据库,更新入库
//做一个数据库的语言对应表

//print_r2($_REQUEST);
//print_r2($la2db);

$to=$_REQUEST['to'];
$_REQUEST['dbname'] = $la2db[$to];

$dblink = MYSQL_CONNECT($dbHostname, $dbUsername, $dbPassword) OR DIE("数据库连接失败!");
mysql_select_db($_REQUEST['dbname']) or die( "Unable to select database ".$_REQUEST['dbname']);




//=====================================
//这应该看看目标数据库有没有这条数据，没有的话，就需要插入。而不是更新。
//这些数据都是不带detail内容的。
//detail同样需要另外请求.不过因为这步已经做了 数据存在判断，
//所以sync_data_detail肯定有数据可以更新，不用插入
//=====================================













//=============
//select看有无这条数据

if($_REQUEST['table']!="site") {
    //只要不是site，都查一下是否有这条数据。

    $_got_other_version = select_one_line_all_items($table_prefix.$_REQUEST['table'],"unid = '".$_target_attr['unid']."'");




    //所有的分类的unid $term_unid;
    //这个数据的unid   $_target_attr['unid'];
    //在新数据库找出对应的term id 和detail id

    $term_unid_1d=transform_2darray_in_1darray_number_as_key($term_unid,"unid");

    $term_unid_str=implode("','",$term_unid_1d);

    //echo $term_unid_str;

    $_track_term_nid=select_lines_items($table_prefix."term","","nid","unid in ('".$term_unid_str."')","");



    echo "查找有没有这个分类";//如果目标没有这个分类的话，就只能放弃关系了。
    print_r2($_track_term_nid);
    #foreach $_track_term_nid 的 [term_nid] 即可获取所有分类的nid


    //关于补充关系。
    //没有这条数据的话，要插入之后再补充关系。
    //有这条数据的话，就直接用nid补充关系。




    if(!$_got_other_version['nid'])
    {//不存在这条数据
        $_insert_trans_str=insert_date($table_prefix.$_REQUEST['table'],$_target_attr_ai);//这里应该插入数据
        echo $_insert_trans_str;
        $query=mysql_query($_insert_trans_str) or die (mysql_error());

        //插入数据之后，再查找这条数据的nid
        $_detail_just_insert = select_one_line_all_items($table_prefix.$_REQUEST['table'],"unid = '".$_target_attr['unid']."'");
        print_r2($_detail_just_insert);

        //拿到了nid了，更新关系
        foreach($_track_term_nid as $k=>$v)
        {
            echo "<br/>";
            $_date['ai_detail_nid']=$_detail_just_insert['nid'];
            $_date['ai_term_nid']  =$v['nid'];
            echo $inser_str=insert_date($table_prefix."relation",$_date);
            echo "<br/>";
            $query=mysql_query($inser_str) or die (mysql_error());
        }


    }
    else
    {//存在这条数据；更新内容。


        $_updatestr=update_date($table_prefix.$_REQUEST['table'],$_target_attr_ai,"unid",$_target_attr['unid']);
        #echo $_updatestr;
        $query=mysql_query($_updatestr) or die (mysql_error());

        //并且用更新的形式，补充关系。
        foreach($_track_term_nid as $k=>$v)
        {
            echo "<br/>";
            $_date['ai_detail_nid']=$_got_other_version['nid'];
            $_date['ai_term_nid']  =$v['nid'];
            echo $inser_str=insert_date($table_prefix."relation",$_date);
            echo "<br/>";
            $query=mysql_query($inser_str) or die (mysql_error());
        }


    }



//插入relation


}
else
{

$_updatestr=update_date($table_prefix.$_REQUEST['table'],$_target_attr_ai,"nid","0");
echo $_updatestr;
$query=mysql_query($_updatestr) or die (mysql_error());

}



?>


